package com.tech.view.panel;

import com.tech.view.common.ColorUtil;
import com.tech.view.common.FontUtil;
import com.tech.view.common.GraphicsUtil;
import com.tech.view.common.ResourcesUtil;
import com.tech.repository.dto.GoodsInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class GoodsItemPanel extends JPanel {
	private int width;
	private int height;
	private GoodsInfo goodsInfo;

	private JLabel image;
	private JLabel name;
	private JLabel price;

	public GoodsItemPanel(GoodsInfo goodsInfo, int width, int height) {
		this.width = width;
		this.height = height;
		this.goodsInfo = goodsInfo;
		initService();
		initComponents();
		setListeners();
		initView();
	}

	private void initService() {
	}

	private void initComponents() {
		BufferedImage firstImage = goodsInfo.getFirstImage();
		if (firstImage != null) {
			firstImage = GraphicsUtil.createScaledCompositeInstance(firstImage, height, false);
		} else { //该商品没有图片时
			firstImage = ResourcesUtil.getImage(ResourcesUtil.Image.DEFAULT_GOODS_IMAGE);
		}
		image = new JLabel(new ImageIcon(firstImage));

		name = new JLabel(goodsInfo.getGoods().getGname());
		name.setFont(FontUtil.TITLE_FONT.deriveFont(36f));
		name.setVerticalAlignment(SwingConstants.TOP);

		price = new JLabel(String.format("￥%.2f", goodsInfo.getGoods().getGsecprice()));
		price.setFont(FontUtil.TITLE_FONT.deriveFont(25f));
		price.setForeground(ColorUtil.RED_LABEL_COLOR);
		price.setVerticalAlignment(SwingConstants.BOTTOM);
	}

	private void initView() {
		setPreferredSize(new Dimension(width, height));
		setBackground(ColorUtil.LIGHT_BACKGROUND_COLOR);
		setLayout(new BorderLayout());

		add(image, BorderLayout.WEST);
		add(name);
		add(price, BorderLayout.EAST);
	}

	private void setListeners() {
	}
}
